﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Box2D.XNA;
using Microsoft.Xna.Framework.Media;

namespace Elementum
{
    class Stage9 : Stage
    {
        public Stage9(Game game) : base(game) { }

        const int FOCUS_TIME = 240;
        int focusTime = FOCUS_TIME;
        float GROUND_ALPHA = 0f;

        SmokePlumeParticleSystem smokePlume;
        const float TimeBetweenSmokePlumePuffs = 0.85f;
        float timeTillPuff = 0.0f;
        //bool _clearSmok = false;


        public override void InitializeScenes()
        {
            base.InitializeScenes();
            for (int i = 1; i < 4; i++)
            {
                Animation a = new Animation("images/stages/stage3/city_night" + i);
                a.LayerDepth = 0.9f;
                _backGround.Add(a);
            }
            _backGroundPos = new Vector2(-435, -200);

            _scenceBound = new Rectangle(40, -100,
               2500,
              500);
            camera.Bound = _scenceBound;
            camera.CameraTransfer = 100;
            camera.MoveSpeed = 5f;
            // basePos = player.Position;
            for (int i = 0; i < 3; i++)
            {
                Animation wallBack = new Animation("images/stages/stage9/stage9_" + i);
                //wallBack.LayerDepth = 0.9f;
                _wallBackGround.Add(wallBack);
            }
            _wallBackGroungPos = new Vector2(-55, -324);

            smokePlume = new SmokePlumeParticleSystem(this, 5);

            Animation title = new Animation("images/title/stageTitle", new Rectangle(2, 238, 138, 44), new Point(138, 44));
            title.Scale = 1.2f;

            Transition tra;
            tra = new DefauldTransition(1, 0, 0, 60);
            tra.Register(this);
            tra = new TitleTransition(title, new Vector2(220, 180), 0, 1, 80, 220);
            tra.Register(this);

            if (MediaPlayer.State == MediaState.Playing)
                MediaPlayer.Stop();

            GroundObject grount =
            StateFactory.CreateGround(this, 81, 288, 100, 300, 0f); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 396, 413, 530, 50, 0f); grount.Alpha = GROUND_ALPHA;
            StateFactory.CreatePortal("Stage9_Portal_1", this, 462, 338, "stage8", "Stage8Portal_2");
            grount =
            StateFactory.CreateGround(this, 711, 413, 100, 50, 0f); grount.Alpha = GROUND_ALPHA;
            grount =
            StateFactory.CreateGround(this, 1211, 442, 899, 25, 0f); grount.Alpha = GROUND_ALPHA;
            grount =
            StateFactory.CreateGround(this, 1829, 353, 338, 20, 0f); grount.Alpha = GROUND_ALPHA;
            grount =
            StateFactory.CreateGround(this, 2147, 368, 160, 50, 0f); grount.Alpha = GROUND_ALPHA;
            grount =
            StateFactory.CreateGround(this, 2568, 368, 537, 50, 0f); grount.Alpha = GROUND_ALPHA;
            grount =
            StateFactory.CreateGround(this, 1479, 88, 26, 246, 0f); grount.Alpha = GROUND_ALPHA;//1480
            grount =
            StateFactory.CreateGround(this, 1560, 88, 26, 246, 0f); grount.Alpha = GROUND_ALPHA;//1540

            grount =
            StateFactory.CreateGround(this, 2140, 227, 80, 19, 0f); grount.Alpha = GROUND_ALPHA;
            SensorSwitch sensorSwitch =
            StateFactory.CreateSensorSwitch(this, 1044, 414); sensorSwitch.MusicOn = false;//sensorSwitch.AddTarget("");
            SensorSwitch sensorSwitch1 =
            StateFactory.CreateSensorSwitch(this, 1380, 414); sensorSwitch1.MusicOn = false;//sensorSwitch.AddTarget("");
            MechanismGound ground =
            StateFactory.CreateMechanismGound(this, 2260, 1278, 70, 10, 0, 0, -1000, 0, 1); sensorSwitch.AddTarget(ground);
            MechanismGound ground1 =
            StateFactory.CreateMechanismGound(this, 2032, 1195, 70, 10, 0, 0, -1000, 0, 1); sensorSwitch1.AddTarget(ground1);
            grount =
            StateFactory.CreateGround(this, 1683, -20, 220, 30, 0f); grount.Alpha = GROUND_ALPHA;
            StateFactory.CreateElementPiece("Stage9_Ele_1", this, 1519, 6);
            grount =
            StateFactory.CreateGround(this, 1805, 191, 280, 20, 0f); grount.Alpha = GROUND_ALPHA;
            StateFactory.CreateDoor("Stage9_Door_S1", this, 1808, 129, "Stage9_Door_1");
            Floor floor =
            StateFactory.CreateFloor(this, 1724, 48, 40, 234, 0, 0, 240, 0, 60);
            Switch gameSwitch =
            StateFactory.CreateSwitch(this, 1894, 156); gameSwitch.AddTarget(floor);
            grount =
            StateFactory.CreateGround(this, 1678, 409, 35, 91, 0f); grount.Alpha = GROUND_ALPHA;
            grount =
            StateFactory.CreateGround(this, 1846, 442, 301, 25, 0f); grount.Alpha = GROUND_ALPHA;
            StateFactory.CreateKey("Stage9_Key_1", this, 1903, 410, "Stage9_Door_1");
            StateFactory.CreatekillBund(this, 2035, 422, 64, 64, 0);
            StateFactory.CreatekillBund(this, 2268, 422, 64, 64, 0);

            StateFactory.CreateElementPiece("Stage9_Ele_2", this, 1142, 414);
            StateFactory.CreateElementPiece("Stage9_Ele_3", this, 719, 373);
            StateFactory.CreateElementPiece("Stage9_Ele_4", this, 1949, 414);
            StateFactory.CreateElementPiece("Stage9_Ele_5", this, 2145, 202);

            ground =
            StateFactory.CreateMechanismGound(this, 2656, 346, 35, 64, 0, 0, 32, 0, 60);
            ground.CurrentAnimation = new Animation("images/items/mechineWall", new Point(35, 64));
            ground1 =
            StateFactory.CreateMechanismGound(this, 2656, 282, 35, 64, 0, 0, -32, 0, 60);

            ground1.CurrentAnimation = new Animation("images/items/mechineWall", new Point(35, 64));
            grount =
            StateFactory.CreateGround(this, 2656, 185, 35, 130, 0f); grount.Alpha = GROUND_ALPHA;
            grount =
            StateFactory.CreateGround(this, 2737, 103, 198, 35, 0f); grount.Alpha = GROUND_ALPHA;
            grount =
            StateFactory.CreateGround(this, 2854, 239, 35, 308, 0f); grount.Alpha = GROUND_ALPHA;
            StateFactory.CreateElementPiece("Stage9_Ele_5", this, 165, 373);
            Mechine mechine =StateFactory.CreateMechine("Stage9_Mechine_1", this, 2513, 303, 6); mechine.AddTarget(ground); mechine.AddTarget(ground1);
            GameClass._mic.SoundLevel = SoundLevel.Severe;
            GameClass._mic.TooLoud += ToodLoudTest;

            ConsoleFinal cf = new ConsoleFinal("finalConsole", _world, this);
            cf.Position = new Vector2(2755, 279-30);
            actors.Add(cf);

            //StateFactory.CreatePortal("Stage9_Portal_2", this, 2650, 279, "stage8", "Stage8Portal_2");

            Monster m;

            for (int i = 0; i < 3; i++)
            {
                m = new Monster(this);
                m.SPEED = new Vector2(1.7f-(0.1f*i), 0);
                m.Position = new Vector2(1527, 170 - 56 * i);//1150, 168
                //m.GetBody().SetLinearVelocity(new Vector2(0,20));
                actors.Add(m);
            }

            InitMonsterMechine();

            MusicManager.AddMusic("Music/doorlock", "doorlock", 0.001f, false);
            _bloom = _gameClass._BloomComponent;
            actors.Add(smokePlume);

            _startProtal = new ProtalCommand("stage9", "Stage9_Portal_1");
        }

        public void StageClear() {
            GameClass._mic.Dispose();

            StageManager.Save = new DataNeedSave();
            Transition tra;        
            tra = new DefauldTransition(0, 1, 10, 50);//black
            tra.Register(this);
            tra = new DefauldTransition(1, 1, 48, 80);//black
            tra.Register(this);
             tra.TransitionDoneAction += GameClear;
             tra = new DefauldTransition(1, 1, 48, 100);//black
             tra.Register(this);
           


        }

        private void UpdateSmokePlume(float dt)
        {
            timeTillPuff -= dt;
            if (timeTillPuff < 0)
            {
                Vector2 where = Vector2.Zero;

                {
                    where.X = GameClass.RandomBetween(760, 1440);
                    where.Y = GameClass.RandomBetween(250, 470);
                }
                smokePlume.AddParticles(where);

                timeTillPuff = TimeBetweenSmokePlumePuffs;
            }
        }

        public void ToodLoudTest()
        {
            smokePlume.BlowAway(player.Position);
        }

        GroundObject _ground;

        public void GameClear()
        {
            StageManager.GameClear = true;
        }

        public List<GroundObject> groundList = new List<GroundObject>();
        public void InitMonsterMechine()
        {

            GroundObject ground =
            StateFactory.CreateGround(this, 1519, 194, 48, 14, 0f); groundList.Add(ground);
            ground.CurrentAnimation = new Animation("images/items/mechineWall1", new Point(48, 14));
            ground =
            StateFactory.CreateGround(this, 1519, 138, 48, 14, 0f); groundList.Add(ground);
            ground.CurrentAnimation = new Animation("images/items/mechineWall1", new Point(48, 14));
            ground =
            StateFactory.CreateGround(this, 1519, 82, 48, 14, 0f); groundList.Add(ground);
            ground.CurrentAnimation = new Animation("images/items/mechineWall1", new Point(48, 14));
            _ground =
            StateFactory.CreateGround(this, 1519, 26, 48, 10, 0f);
            _ground.CurrentAnimation = new Animation("images/items/mechineWall1", new Point(48, 14));
            
            _ground.BeginContact += GroundFall;

            for (int i = 0; i < 3; i++)
            {
                ModifierContain first = new ModifierContain(groundList[i]);
                PositionModifier m2 = new PositionModifier(groundList[i].Position, groundList[i].Position + new Vector2(2000, 0), 60 + (i * 75), 60 + (i * 75) + 1);
                first.AddModifier(m2);
                groundList[i].ModifierContainer = first;
            }

            FocusTarget f = new FocusTarget(new Vector2(1530, 250));
            camera.Focus = f;

        }

        public void GroundFall(Contact contact, Body contactBody, float normal)
        {
            if (contactBody.GetUserData() == "player")
            {
                _ground.UpDateAction += dispeear;
            }
        }

        public void dispeear()
        {
            _ground.GetBody().SetType(BodyType.Dynamic);
            Filter filter = new Filter();
            filter.categoryBits = 0x00;
            filter.maskBits = 0x00;
            _ground._fixture.SetFilterData(ref filter);

        }

        public override void Restart()
        {
            foreach (VisibleObject b in actors)
            {
                b.Restart();
            }
            //StageManager.ProtalCommand = _startProtal;
        
            //base.Restart();
            //StageManager.Save.StageStateTable.Remove(StageManager.CurrentStateID);
            //StageManager.ProtalCommand = new ProtalCommand("stage9", "Stage9_Portal_1");
        }

        public override void OnDraw(SpriteBatch spriteBatch, int delta)
        {

            base.OnDraw(spriteBatch, delta);


            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.Transform);
            // basePos = player.Position;

            foreach (VisibleObject b in actors)
                b.OnDraw(spriteBatch, camera.Transform);

            spriteBatch.End();
            //DrawWalls(spriteBatch);
            //spriteBatch.Begin();
            //spriteBatch.DrawString(Font, "Po" + mechanismGound.PositiveActionCount + "Ne" + mechanismGound.NegetiveActionCount, new Vector2(50, 50), Color.Black);
            //spriteBatch.DrawString(Font, "free"+ smokePlume.FreeParticleCount, new Vector2(150, 100), Color.Black);
            //spriteBatch.End();

        }

        public override void Update(int delta)
        {
            UpdateSmokePlume(0.07f);
            base.Update(delta);
            if (camera.Focus != player && focusTime > 0)
                focusTime--;
            if (focusTime == 0)
                camera.Focus = player;
            //camera.Update(delta);
            GameClass._mic.Update();
            //a.Update(delta);
            //  MusicManager.FadeInMusic("first");
            //MusicManager.FadeOutMusic("first");
        }

        public override Stage Clone()
        {
            Stage9 stage = new Stage9(Game);
            stage.InitializeScenes();
            return stage;
        }
    }
}
